package com.smartalarmclock.Model;

import java.util.HashSet;
import java.util.Set;

import android.content.Context;

import com.smartalarmclock.Model.Exceptions.DataEntityDoesNotExistException;

public final class AutoResponseFactory extends Object implements IReferencingDataEntityFactory {

	/**
	 * Creates a new AutoResponse instance
	 * @param isBlocking whether the auto response should block the call
	 * @param shouldSendToAnsweringMachine whether the call should be sent to the answering machine
	 * @param smsToSend the sms messsage to send automatically
	 * @return a new AutoResponse instance.
	 */
	public AutoResponse CreateAutoResponse(boolean isBlocking, boolean shouldSendToAnsweringMachine, ISMSMessage smsToSend) {
		return new AutoResponse(isBlocking, shouldSendToAnsweringMachine, smsToSend);
	}
	
	/**
	 * Creates a new AutoResponse instance with the default values
	 * @return a new AutoResponse instance.
	 */
	public AutoResponse CreateAutoResponse() {
		return new AutoResponse();
	}
	
	/**
	 * Queries the database for an AutoResponse instance with the matching id
	 * @param context the context of the request
	 * @param id the id to query
	 * @return the matching AutoResponse instance
	 * @throws DataEntityDoesNotExistException if the data entity does not exist
	 */
	public IAutoResponse GetAutoResponseById(Context context, int id) throws DataEntityDoesNotExistException {
		return AutoResponse.GetAutoResponseById(context, id);
	}
	
	/**
	 * Queries the database for an AutoResponse instance with the matching id
	 * @param context the context of the request
	 * @param id the id to query
	 * @return the matching AutoResponse instance, or null it it does not exist
	 */
	public IAutoResponse TryGetAutoResponseById(Context context, int id) {
		return TryGetAutoResponseById(context, id);
	}
	
	/**
	 * Queries the database for AutoResponse instances using the given SMS message
	 * @param context the context of the request
	 * @param message the sms message to query
	 * @return a set of matching AutoResponse instances
	 */
	public Set<IAutoResponse> GetAutoResponseBySMSMessage(Context context, ISMSMessage message) {
		return AutoResponse.GetAutoResponsesBySMSMessage(context, message);
	}
	
	@Override
	public Set<IReferencingDataEntity> GetReferencingEntities(Context context, IReferencedDataEntity referencedDataEntity) throws DataEntityDoesNotExistException {
		HashSet<IReferencingDataEntity> referencingEntities = new HashSet<IReferencingDataEntity>();
		for (IAutoResponse autoResponse : GetAutoResponseBySMSMessage(context, (ISMSMessage)referencedDataEntity)) {
			referencingEntities.add(autoResponse);
		}
		
		return referencingEntities;
	}
}
